package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;

import com.breadtrip.R;

/**
 * Created by Administrator on 2016/11/1.
 */
public class PorterDuffSrcTtopTextView extends android.support.v7.widget.AppCompatTextView {
    private static final boolean DEBUG = true;
    private static final String TAG = "PorterDuffSrcTtop";

    private static final int LIGHT_COLOR = 0xffffffff;
    private static final int TEXT_COLOR = 0xff2196f3;
    private Paint mPaint;
    private Paint mPaint2;
    private int mLightColor = LIGHT_COLOR;
    private int mTextColor = TEXT_COLOR;

    public PorterDuffSrcTtopTextView(Context context) {
        super(context);
        init(context);
    }

    public PorterDuffSrcTtopTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public PorterDuffSrcTtopTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void init(Context context) {
        mPaint = getPaint();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        initValues();
        if (mPaint == null) {
            if (DEBUG) {
                Log.i("test2", "mpain == null");
            }
            return;
        }
        moveHeight += height / 20;
        if (moveHeight >= height) {
            moveHeight = 0;
        }
        //将绘制代码放入到canvas.saveLayer()和canvas.restore()之间
        //layer1
        int layer = canvas.saveLayer(0, 0, width, height, mPaint2, Canvas.ALL_SAVE_FLAG);

        int layer3 = canvas.saveLayer(0, 0, width, height, mPaint2, Canvas.ALL_SAVE_FLAG);

        mPaint.setColor(0xffffffff);
        canvas.drawCircle(width / 2, height / 2, width / 2, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        mPaint.setColor(TEXT_COLOR);
        canvas.drawRect(0, height - moveHeight, width, height * 2 - moveHeight, mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(layer3);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //可以省略
        mPaint.setXfermode(null);
        canvas.restoreToCount(layer);


        //layer2
        int layer2 = canvas.saveLayer(0, 0, width, height, mPaint2, Canvas.ALL_SAVE_FLAG);
        super.onDraw(canvas);
        mPaint.setColor(LIGHT_COLOR);
        //将画笔的Xfermode设置为PorterDuff.Mode.SRC_ATOP模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        //用LinearGradient绘制矩形

        canvas.drawRect(0, height - moveHeight, width, height * 2 - moveHeight, mPaint);
        //最后将画笔去除掉Xfermode
        mPaint.setXfermode(null);
        canvas.restoreToCount(layer2);


        postInvalidateDelayed(100);
        if (DEBUG) {
            Log.i("test2", "PorterDuffSrcTtopTextView");
        }
    }

    private int moveHeight = -1;
    private int width = -1;
    private int height = -1;

    private void composeshader(Canvas canvas) {
        initValues();
        if (mPaint == null) {
            if (DEBUG) {
                Log.i("test2", "mpain == null");
            }
            return;
        }
        //将绘制代码放入到canvas.saveLayer()和canvas.restore()之间
        canvas.saveLayer(0, 0, width, height, mPaint2, Canvas.MATRIX_SAVE_FLAG
                | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
        mPaint.setTextSize(getTextSize());
        canvas.drawText("89%", width / 2, height / 2, mPaint);
        //将画笔的Xfermode设置为PorterDuff.Mode.SRC_ATOP模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        //创建LinearGradient，用以产生从左上角到右下角的颜色渐变效果
        LinearGradient linearGradient = new LinearGradient(0, 0, width, height, mLightColor, mTextColor, Shader.TileMode.CLAMP);
        //将创建LinearGradient作为画笔paint绘图所使用的shader
        mPaint.setShader(linearGradient);
        //用LinearGradient绘制矩形
        moveHeight += height / 20;
        if (moveHeight >= height) {
            moveHeight = 0;
        }
        canvas.drawRect(0, height - moveHeight, width, height * 2 - moveHeight, mPaint);
        //最后将画笔去除掉Xfermode
        mPaint.setXfermode(null);
        canvas.restore();
        postInvalidateDelayed(100);
        if (DEBUG) {
            Log.i("test2", "PorterDuffSrcTtopTextView");
        }
    }


    private void initValues() {
        if (moveHeight == -1) {
            moveHeight = 0;
        }
        if (width == -1) {
            width = getWidth();
        }
        if (height == -1) {
            height = getHeight();
        }

        if (mPaint == null) {
            mPaint = getPaint();
        }

        if (mPaint2 == null) {
            mPaint2 = new Paint();
            mPaint2.setAntiAlias(true);
        }
    }
}
